package com.rzico.sbl.ui.statistic

import com.lzg.extend.jackson.JsonUtil
import com.lzy.okgo.model.HttpMethod
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityStockStoreDetailBinding
import com.rzico.sbl.model.BaseUrl
import com.rzico.sbl.model.StoreGoods
import com.rzico.sbl.viewmodel.StatisticStockViewModel
import com.xinnuo.common.decoration.SpaceTBDecoration
import com.xinnuo.common.extend.formatDecimal
import com.xinnuo.common.extend.get
import com.xinnuo.common.extend.getExtra
import com.xinnuo.common.extend.optNotNull
import com.xinnuo.common.extend.textDouble
import com.xinnuo.common.helper.getTimeString
import com.xinnuo.common.view.FullyLinearLayoutManager
import com.xinnuo.common_ui.base.BaseActivity
import com.xinnuo.common_ui.base.addItems
import com.xinnuo.slimadapter.SlimAdapter
import org.json.JSONArray
import org.json.JSONObject

class StockStoreDetailActivity : BaseActivity(R.layout.activity_stock_store_detail) {

    private val mBinding by lazy { ActivityStockStoreDetailBinding.bind(getRootView()) }

    override fun getViewModel() = get<StatisticStockViewModel>()

    override fun initData() {
        initTitle(title = "采购详情")

        initLayout()

        getData()
    }

    private fun initLayout() {
        mBinding
            .storeList
            .apply {
                isNestedScrollingEnabled = false
                layoutManager = FullyLinearLayoutManager(getIContext())

                addItemDecoration(SpaceTBDecoration())

                mAdapter = SlimAdapter.creator()
                    .register<StoreGoods>(R.layout.item_stock_store_detail) { jector, bean, _ ->
                        jector.apply {
                            text(R.id.item_store_name, bean.name)
                            text(R.id.item_store_num, bean.quantity)
                            text(R.id.item_store_price, bean.price.formatDecimal())
                        }
                    }
                    .attachTo(this)
            }

        mAdapter.setDataList(mList)
    }

    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .request(
                requestUrl = BaseUrl.psiStoreDet,
                requestMethod = HttpMethod.GET,
                params = arrayOf("id" to intent.getExtra("storeId")),
                onSuccess = { result ->
                    val obj = JSONObject(result)

                    mBinding.apply {
                        storeSupply.text = obj.optNotNull("supplierName")
                        storeShop.text = obj.optNotNull("shopName")
                        storeSn.text = obj.optNotNull("sn")
                        storeCreate.text =
                            obj.optNotNull("createDate")
                                .getTimeString()
                        storeCredit.text =
                            obj.optNotNull("creditDate")
                                .getTimeString()
                        storeOperator.text = obj.optNotNull("operator")
                        storeMemo.text = obj.optNotNull("memo")

                        storeFee.text = obj.optNotNull("freight").formatDecimal()
                        storeAppend.text = obj.optNotNull("payAmount").formatDecimal()
                        storePay.text = obj.optNotNull("amountPaid").formatDecimal()
                        storeCash.text = obj.optNotNull("cashPay").formatDecimal()
                        storeWechat.text = obj.optNotNull("weixinPay").formatDecimal()
                        storeAlipay.text = obj.optNotNull("aliPay").formatDecimal()
                        storeBack.text = obj.optNotNull("repayment").formatDecimal()
                        storeNew.text = obj.optNotNull("arrears").formatDecimal()

                        // 累计欠款
                        val amountNew = storeNew.textDouble
                        // 应付金额
                        val amountThis =
                            (storeFee.textDouble
                                    + storeAppend.textDouble
                                    + storePay.textDouble)
                        // 支出金额
                        val amountOut =
                            (storeCash.textDouble
                                    + storeWechat.textDouble
                                    + storeAlipay.textDouble)

                        storeAll.text = amountThis.formatDecimal()
                        storeOld.text = (amountNew - amountThis + amountOut).formatDecimal()

                        /*---------------------- 商品信息 ----------------------*/
                        val objOrder = obj.optJSONArray("creditItems") ?: JSONArray()
                        mList.clear()
                        mList.addItems(
                            JsonUtil.toList(
                                objOrder.toString(),
                                StoreGoods::class.java
                            )
                        )
                        mAdapter.notifyDataSetChanged()
                    }
                }
            )
    }

}